package com.hkyp.ss.course.provider.impl;

import com.hkyp.ss.course.apis.UserCourseRelationService;
import com.hkyp.ss.course.apis.dto.common.PageReq;
import com.hkyp.ss.course.apis.dto.common.PageResp;
import com.hkyp.ss.course.apis.dto.response.UserHPCourseVO;
import com.hkyp.ss.course.apis.enumu.DeletedEnum;
import com.hkyp.ss.course.apis.enumu.YesNoEnum;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.provider.model.Course;
import com.hkyp.ss.course.provider.model.CourseComment;
import com.hkyp.ss.course.provider.model.mediate.UserCourseCollection;
import com.hkyp.ss.course.provider.model.mediate.UserCourseStudy;
import com.hkyp.ss.course.provider.repository.CourCommRepository;
import com.hkyp.ss.course.provider.repository.CourseRepository;
import com.hkyp.ss.course.provider.repository.mediate.UserCourCollRepository;
import com.hkyp.ss.course.provider.repository.mediate.UserCourStudyRepository;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service(version = "1.0")
@Transactional(rollbackFor = Exception.class)
public class UserCourseRelationServiceImpl implements UserCourseRelationService {

    @Autowired
    UserCourCollRepository userCourCollRepository;

    @Autowired
    CourseRepository courseRepository;

    @Autowired
    CourCommRepository courCommRepository;
    @Autowired
    private UserCourStudyRepository courStudyRepository;

    @Override
    @Transactional(readOnly = true)
    public PageResp<UserHPCourseVO> selectCourseByUserCollect(PageReq pageReq, Long userId) {

        UserCourseCollection userCourseCollection = new UserCourseCollection();
        userCourseCollection.setUserId(userId);

        Example<UserCourseCollection> courseCommentExample = Example.of(userCourseCollection);
        Page<UserCourseCollection> pageResp = userCourCollRepository.findAll(courseCommentExample,pageReq.toPageable());
        List<UserCourseCollection> courseCollections = pageResp.getContent();
        //本页的课程id集合
        Set<Long> courIdsSet = courseCollections.stream().map(UserCourseCollection::getCourseId).collect(Collectors.toSet());

        List<Course> courseList = courseRepository.findAllById(courIdsSet);

        List<UserHPCourseVO> userHPCourseVOS = BeanMapper.mapList(courseList, UserHPCourseVO.class);
        // cid:collection.createTime map
        Map<Long, Long> collectionCreateAtMap = courseCollections.stream().collect(Collectors.toMap(UserCourseCollection::getCourseId, UserCourseCollection::getCreateAt));

        /**
         * 用户是否撰写过课评、是否学习过此课程
         */
        Map<Long, Long> courseCommentMap = new HashMap<>();
        Map<Long, UserCourseStudy> userCourseStudyMap = new HashMap<>();
        //是否写过课评
        List<CourseComment> courseCommentList = courCommRepository.findAllByCourseIdInAndUserIdEqualsAndDeletedEquals(courIdsSet, userId, DeletedEnum.NO.getCode());
        if(!CollectionUtils.isEmpty(courseCommentList)){
            courseCommentMap = courseCommentList
                    .stream()  // 构建 cid:ccid 的map
                    .collect(Collectors.toMap(CourseComment::getCourseId, CourseComment::getId));
        }
        //是否学习过
        List<UserCourseStudy> userCourseStudyList = courStudyRepository.findAllByUserIdAndCourseIdInAndStatusEquals(userId, courIdsSet, YesNoEnum.NO.getCode());
        if(!CollectionUtils.isEmpty(userCourseStudyList)){
            userCourseStudyMap = userCourseStudyList
                    .stream()
                    .collect(Collectors.toMap(UserCourseStudy::getCourseId, obj -> obj));
        }
        Map<Long, UserCourseStudy> userCourseStudyMapLam = userCourseStudyMap;
        Map<Long, Long> courseCommentMapLam = courseCommentMap;
        userHPCourseVOS.forEach(responseDTO -> {
            //是否写过课评
            Long ccid   = courseCommentMapLam.get(responseDTO.getId());
            if(ccid!=null){
                responseDTO.setEdited(YesNoEnum.YES.getCode());
                responseDTO.setCcId(ccid);
            }
            //是否学习过
            if(userCourseStudyMapLam.get(responseDTO.getId())!= null){
                responseDTO.setStudied(YesNoEnum.YES.getCode());
            }
            //设置收藏时间
            responseDTO.setCreateAt(collectionCreateAtMap.get(responseDTO.getId()));
        });

        return new PageResp<>(pageResp,userHPCourseVOS);
    }


}
